package com.buer.storage.business.service.impl;


import com.buer.storage.business.dao.WorderDao;
import com.buer.storage.business.domain.Stock;
import com.buer.storage.business.domain.Worderdetails;
import com.buer.storage.business.dto.PutStorageAllDto;
import com.buer.storage.business.dto.PutStorageDto;
import com.buer.storage.business.dto.WorderDto;
import com.buer.storage.business.dto.WorderdetailsDto;
import com.buer.storage.business.service.WorderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class WorderServiceImpl implements WorderService {
    @Resource
    private WorderDao worderDao;

    //增加入库信息
    @Transactional
    @Override
    public int insert(PutStorageDto putStorageDto) {
//        putStorageDto.setPutStatus(0);
        int result = worderDao.insertOrder(putStorageDto);
        for (int i = 0; i < putStorageDto.getPutProds().length; i++) {
            Worderdetails putProd = putStorageDto.getPutProds()[i];
            putProd.setWorderPutid(putStorageDto.getPutId());
        }
        worderDao.insertDetails(putStorageDto.getPutProds());
        return result;
    }

    //查询所有订单表信息
    @Override
    public List<WorderDto> findOrderAll() {
        List<WorderDto> orderAll = worderDao.findOrderAll();
        return orderAll;
    }

    //根据订单表id删除入库信息
    @Transactional
    @Override
    public int deletePutStorageById(int putId) {
        int deleteWorderById = worderDao.deletePutStorageById(putId);
        return deleteWorderById;
    }


    //根据商品id删除商品信息
    @Override
    public int deleteWorderdetails(int detailsId) {
        int deleteWorderdetails = worderDao.deleteWorderdetails(detailsId);
        return deleteWorderdetails;
    }

    //更新商品id
    @Override
    @Transactional
    public int updateWorderdetails(PutStorageDto putStorageDto) {
        // 第一步先删除,此处需要前段讲putId的值传入过来
        Integer putId = putStorageDto.getPutId();
        worderDao.deleteWorderByPutId(putId);
        worderDao.deleteWorderDetailsByPutId(putId);
        // 第二部再插入
        putStorageDto.setPutStatus(0);
        int result = worderDao.insertOrder(putStorageDto);
        for (int i = 0; i < putStorageDto.getPutProds().length; i++) {
            Worderdetails putProd = putStorageDto.getPutProds()[i];
            putProd.setWorderPutid(putStorageDto.getPutId());
        }
        worderDao.insertDetails(putStorageDto.getPutProds());
        return result;
    }

//    //入库单表增加
//    @Override
//    @Transactional
//    public int submitAddBuerStock(PutStorageDto putStorageDto) {
//        Worderdetails[] putProds = putStorageDto.getPutProds();
//        int result = 0;
//        for (int i = 0; i < putProds.length; i++) {
//            result = 1;
//            Worderdetails putProd = putProds[i];
//            //先查找prodArea,prodDepart,prodId
//            Stock stock = worderDao.queryBuerStockByParam(putProd);
//            if(stock == null){
//                //如果stock表里没有的话就插入
//                worderDao.insertStock(putProd);
//            }else{
//                //更新累加
//                Integer prodNum = putProd.getProdNum();
//                Integer newProdNum = prodNum + stock.getProdStocknum();
//                stock.setProdStocknum(newProdNum);
//                worderDao.updateAddSumStock(stock);
//            }
//            putStorageDto.setPutStatus(1);
//
//        }
//        return result;
//    }


//    @Override
//    public int insertStock(Worderdetails[] worderdetails) {
//
//        for (int i = 0; i <worderdetails.length ; i++) {
//            if (worderDao.selectById(worderdetails[i].getDetailsId())!=null){
//               worderDao.update(worderdetails[i].getDetailsId());
//            }else {
//                int insertStock = worderDao.insertStock(worderdetails[i]);
//           }
//        }
//        return 1;
//    }

//    @Override
//    public int updatePutStorage(int detailsId) {
//        PutStorageDto putStorageDto = new PutStorageDto();
//        if (worderDao.deleteWorderdetails(detailsId) > 0) {
//            int result = worderDao.insertOrder(putStorageDto);
//            for (int i = 0; i < putStorageDto.getPutProds().length; i++) {
//                Worderdetails putProd = putStorageDto.getPutProds()[i];
//                putProd.setWorderPutid(putStorageDto.getPutId());
//            }
//            worderDao.insertDetails(putStorageDto.getPutProds());
//            return result;
//        }

        //根据订单表id查询入库信息
        @Override
        public List<PutStorageAllDto> findPutAll (Integer putId){
                List<PutStorageAllDto> all = worderDao.findPutAll(putId);
                if (all.size() > 0) {
                for (int i = 0; i < all.size(); i++) {
                    WorderdetailsDto[] info = worderDao.findOrderDetailInfoByPutId(all.get(i).getPutId());
                    if (info.length > 0) {
                        all.get(i).setPutProds(info);
                    }
                }

            }
            return all;
        }

    //入库单表增加
    @Override
    @Transactional
    public int submitAddBuerStock(Integer putId) {
        WorderdetailsDto[] putProds = worderDao.findWordertailsAll(putId);

        worderDao.updateWorderStatus(putId);

        int result = 0;
        for (int i = 0; i < putProds.length; i++) {
            result = 1;
            WorderdetailsDto putProd = putProds[i];
            //先查找prodArea,prodDepart,prodId
            Stock stock = worderDao.queryBuerStockByParam(putProd);
            if(stock == null){
                //如果stock表里没有的话就插入
                worderDao.insertStock(putProd);
            }else{
                //更新累加
                Integer prodNum = putProd.getProdNum();
                Integer newProdNum = prodNum + stock.getProdStocknum();
                stock.setProdStocknum(newProdNum);

                worderDao.updateAddSumStock(stock);
            }
        }

        return result;
    }
}
